package com.example.anydo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.anydo.R;
import com.example.anydo.bean.CommonBean;
import com.example.anydo.constant.Url;
import com.example.anydo.net.DataBack;
import com.example.anydo.net.NetClient;
import com.example.anydo.util.pub_util.Util;

public class RegisterActivity extends AppCompatActivity {

    private ImageView back;
    private EditText userName;
    private EditText psw;
    private EditText pswCheck;
    private Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        back=(ImageView)findViewById(R.id.back);
        userName=(EditText)findViewById(R.id.userName);
        psw=(EditText)findViewById(R.id.psw);
        pswCheck=(EditText)findViewById(R.id.pswCheck);
        register=(Button)findViewById(R.id.register);

        back.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                finish();
            }
        });

        register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String name=userName.getText().toString();
                String oldPsw=psw.getText().toString();
                String newPsw=pswCheck.getText().toString();
                if(checkInfo(name, oldPsw, newPsw)){
                    startRegister(name,oldPsw);
                }
            }
        });
    }

    private void startRegister(String name, String psw) {
        NetClient.getInstance()
                .withUrl(Url.BASE_URL + Url.REGISTER_URL)
                .activity(this)
                .setPlayer("请稍后")
                .addParams("name",name)
                .addParams("password",psw)
                .asObject(CommonBean.class)
                .doGet(new DataBack<CommonBean>() {
                    @Override
                    public void onResponse(CommonBean data) {

                        if(data.getCode() == 200){
                            Util.toast(RegisterActivity.this,data.getMessage());
                            finish();
                        }else {
                            Util.toast(RegisterActivity.this,data.getMessage());
                        }
                    }
                    @Override
                    public void onFailure(String msg) {
                        Util.toast(RegisterActivity.this,"出了一点点问题...");
                    }
                });
    }

    private boolean checkInfo(String name,String oldPsw, String newPsw){

        if(name.equals("")){
            Util.toast(this,"请输入账号");
            return false;
        }else if(oldPsw.equals("")){
            Util.toast(this,"请输入密码");
            return false;
        }else if(newPsw.equals("")) {
            Util.toast(this, "请验证密码");
            return false;
        } else if(name.length()<6){
            Util.toast(this,"账号至少为6位字符");
            return false;
        }else if(oldPsw.length()<6 || newPsw.length()<6){
            Util.toast(this,"密码至少为6位字符");
            return false;
        }else if(!oldPsw.equals(newPsw)){
            Util.toast(this,"两次输入密码不一致");
            return false;
        }
        return true;
    }
}